import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:dingtalk/core/model/message_item_model.dart';
import 'package:dingtalk/core/sqflite/chat_message_record_data.dart';
import 'package:equatable/equatable.dart';

part 'message_record_event.dart';

part 'message_record_state.dart';

class THMessageRecordBloc
    extends Bloc<THMessageRecordEvent, THMessageRecordState> {
  THMessageRecordBloc() : super(THMessageRecordInitial());

  @override
  Stream<THMessageRecordState> mapEventToState(
    THMessageRecordEvent event,
  ) async* {
    if (event is THMessageRecordListEvent) {
      List<THMessageItemModel> recordList =
          await ChatMessageRecordProvider.instance.queryChatMessageRecord();

      recordList?.sort(
          (left, right) => right?.unReadCount?.compareTo(left?.unReadCount));
      recordList?.sort((left, right) => right?.time?.compareTo(left?.time));

      recordList?.sort((left, right) => right?.isStick ?? 1);

      yield THMessageRecordList(recordList: recordList);
    }
  }
}
